package com.eascs.crm.nsEditDetail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.O2oMainStoreInfoEntity;
import com.eascs.crm.nsEditDetail.service.IMainStoreInfoService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@Controller
@RequestMapping("/mainStoreInfoView")
public class MainStoreInfoViewController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(MainStoreInfoViewController.class);
	
	@Reference
	IMainStoreInfoService mainStoreInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;
	
	
	/**
	 * @Description:
	 *               <p>
	 *                查询仓库信息
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/query_view", method = RequestMethod.GET)
	public String queryIndex(String loanapplyNo) {
		logger.info("经营主体查询loanapplyNo：【{}】", loanapplyNo);
		O2oMainStoreInfoEntity info = mainStoreInfoService.getByLoanNo(loanapplyNo);
		ViewContext.put("view", info);
		return "/nsEditDetail/main_store_info_view";
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               查询仓库列表
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/list")
	public String customerShopInfoList(String loanapplyNo, @RequestParam(defaultValue="0") Integer showPage, String type, String approvalStatus) {
		logger.info("查询门店信息，贷款单号：【{}】", loanapplyNo);
//		String pageUrl = "/bank/account/mgr?";
		String[] titles = {"仓库性质", "仓库类型", "租赁合同起止日期", "仓库地址" };
		SelectDataOption[] selectDataOptions = { new SelectDataOption("storeNature"), new SelectDataOption("storeType"), null, null};
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
//		PageResult<Object[]> pageResult = mainStoreInfoService.page(loanapplyNo);
//		PageResult<Object[]> pageResult = mainStoreInfoService.pageSensitive(loanapplyNo,type);// 【脱敏】
		PageResult<Object[]> pageResult = mainStoreInfoService.pageSensitiveAS(loanapplyNo,type,approvalStatus);// 【脱敏】

		Button detail = ButtonHelper.createAjaxButton("详情", "/mainStoreInfoRest/detailPage?type="+type+"&approvalStatus="+approvalStatus+"&storeId=");
		Button update = null;
		Button del = null;
		Button addressButton = null;
		
		if(StringUtil.isNotBlank(approvalStatus)){
			if (!"UNDO".equals(approvalStatus)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/mainStoreInfoRest/view_address?storeId=", "UpdateInternalInfo");
			}
		}else {
			if (!"detail".equals(type)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/mainStoreInfoRest/view_address?storeId=", "UpdateInternalInfo");
				if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
					update = ButtonHelper.createAjaxButton("修改", "/mainStoreInfoRest/updatePage?storeId=", "UpdateInternalInfo");
					del = ButtonHelper.createAjaxConfirmButton("删除", "/mainStoreInfoRest/del?storeId=", "UpdateInternalInfo");
					del.setConfirmMessage("确定删除?");
				}
			}
		}
		
		Button[] detailButton = new Button[] {detail, update, del};
		
		
		Button[] titleButtons = new Button[] { null, null, null, addressButton };

//		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildRowNumber(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		ViewContext.put("paginationInfo", paginationInfo);

		return "nsEditDetail/main_store_info_list";

	}

}
